home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 …ember: Reference Library / Dev.CD Dec 98 RL1.toast / Technical Documentation / develop / Additional Articles / Developing Symbiotic Apps / Symbiotic Samples / UAppleEvents.cp Folder / CAERepeater.cp next >
Encoding:
Text File  |  1996-10-28  |  5.5 KB  |  208 lines  |  [TEXT/CWIE]

  1. #include <stdio.h>
  2. #include <SIOUX.h>
  3.  
  4. #include <AEPrint.h>
  5.  
  6. #include <LPreferencesFile.h>
  7. #include <UEventUtils.h>
  8. #include <UResourceMgr.h>
  9.  
  10. #include "StNotificationRecord.h"
  11. #include "StPPCBrowser.h"
  12. #include "StSpinningCursor.h"
  13. #include "UAppleEvents.h"
  14. #include "UProcesses.h"
  15.  
  16. #include "CAERepeater.h"
  17. #include "CLauncherWindow.h"
  18. #include "RheaConstants.h"
  19. #include "SlurpeeCreators.h"
  20.  
  21. // static member variables
  22. Boolean CAERepeater::sDebuggingEnabled = false;
  23.  
  24. CAERepeater::CAERepeater(LModelObject* inDefaultModel)
  25.     : LModelDirector(inDefaultModel)
  26. {
  27.     // install a generic handler
  28.     ThrowIfOSErr_(::AEInstallEventHandler(typeWildCard, typeWildCard, 
  29.                         NewAEEventHandlerProc(LModelDirector::AppleEventHandler),
  30.                         0, false));
  31.  
  32.     // install recording handlers
  33.     ThrowIfOSErr_(UAppleEvents::InitRecordability());
  34.                         
  35.     // set-up SIOUX
  36.     SIOUXSetTitle("\pDebug Window");
  37.     SIOUXSettings.initializeTB = false;
  38.     SIOUXSettings.standalone = false;
  39.     SIOUXSettings.setupmenus = false;
  40.     SIOUXSettings.autocloseonquit = true;
  41.     SIOUXSettings.asktosaveonclose = false;
  42.     SIOUXSettings.showstatusline = false;
  43.     SIOUXSettings.toppixel = 0;
  44.     SIOUXSettings.leftpixel = 0;
  45.     
  46.     // check for the other three finger salute
  47.     EventModifiers lModifiers = UEventUtils::GetModifiers();
  48.     if ((lModifiers & controlKey) && (lModifiers & optionKey) && (lModifiers & cmdKey))
  49.     {
  50.         sDebuggingEnabled = true;
  51.         printf("Debugging enabled\n");
  52.     }
  53. }
  54.  
  55. CAERepeater::~CAERepeater(void)
  56. {
  57. }
  58.  
  59. void CAERepeater::HandleAppleEvent(const AppleEvent& inAppleEvent, AppleEvent& outReply, Int32 inRefCon)
  60. {
  61.     PrintAEDesc(inAppleEvent);
  62.     
  63.     switch(inRefCon)
  64.     {
  65.         case ae_OpenApp:
  66.         case ae_Quit:
  67.         {
  68.             // these are special, just pass them on to the default model (the application)
  69.             StAEDescriptor lDummyResult;
  70.             
  71.             LModelObject::GetDefaultModel()->HandleAppleEvent(inAppleEvent, outReply, lDummyResult, inRefCon);
  72.         }
  73.         break;
  74.         
  75.         default:
  76.         {
  77.             // whack up a wait cursor
  78.             StSpinningBeachBallCursor lBeachBall;
  79.  
  80.             // get the original sender
  81.             StAEDescriptor lSender;
  82.             ThrowIfOSErr_(::AEGetAttributeDesc(&inAppleEvent, keyOriginalAddressAttr, typeWildCard, lSender));
  83.             
  84.             // create the address descriptor
  85.             StAEDescriptor lTargetAddress;
  86.             ThrowIfOSErr_(::AECreateDesc(typeTargetID, 
  87.                         &(CLauncherWindow::GetConnection()->GetTargetID()), 
  88.                         sizeof(TargetID), lTargetAddress));
  89.  
  90.             // duplicate the event and change address
  91.             StAEDescriptor lAppleEvent;
  92.             ThrowIfOSErr_(::AEDuplicateDesc(&inAppleEvent, lAppleEvent));
  93.             ThrowIfOSErr_(::AEPutAttributeDesc(lAppleEvent, keyAddressAttr, lTargetAddress));
  94.             
  95.             // get the timeout if it exists
  96.             long lTimeout = kAEDefaultTimeout;
  97.             {
  98.                 StAEDescriptor lTimeoutDesc;
  99.                 if (::AEGetAttributeDesc(&inAppleEvent, keyTimeoutAttr, typeWildCard, lTimeoutDesc) == noErr)
  100.                 {
  101.                     UExtractFromAEDesc::TheInt32(lTimeoutDesc, lTimeout);
  102.                 }
  103.             }
  104.             
  105.             // send the apple event, waiting for the reply
  106.             StAEDescriptor lReply;
  107.             OSErr lSendErr = UAppleEvents::SendWaitReply(lAppleEvent, lReply, lTimeout);
  108.             
  109.             // send the apple event to ourselves for recording
  110.             UAppleEvents::Record(inAppleEvent);
  111.             
  112.             // duplicate the reply and re-address
  113.             ThrowIfOSErr_(::AEDuplicateDesc(lReply, &outReply));
  114.             ThrowIfOSErr_(::AEPutAttributeDesc(&outReply, keyAddressAttr, lSender));
  115.             
  116.             ThrowIfOSErr_(lSendErr);
  117.         }
  118.         break;
  119.     }
  120.     
  121.     PrintAEDesc(outReply);
  122. }
  123.  
  124. void CAERepeater::OpenConnection(TargetID& outTarget)
  125. {
  126.     // create a notification record for use with AEInteractWithUser
  127.     StNotificationRecord lNotificationRecord;
  128.  
  129.     // attempt to get the last target address from a preference file
  130.     LStr255 lPrefFileName(ResID_ConfigStrings, configStrings_PrefFileName);
  131.     LPreferencesFile lPreferences(lPrefFileName);
  132.     lPreferences.OpenOrCreateResourceFork(fsRdWrPerm, sig_Rhea, fileType_PrefFile, smSystemScript);
  133.     Boolean lDefaultSpecified = false;
  134.     {
  135.         StResource lResource(typeTargetID, ResID_TargetPreference, false, true);
  136.         if (lResource.mResourceH != nil)
  137.         {
  138.             ::BlockMoveData(*lResource.mResourceH, &outTarget, sizeof(TargetID));
  139.             lDefaultSpecified = true;
  140.         }
  141.     }
  142.  
  143.     // browse
  144.     SFTypeList lTypes;
  145.     lTypes[0] = sig_Sandra;
  146.     LStr255 lPrompt(ResID_ConfigStrings, configStrings_BrowserPrompt);
  147.     LStr255 lAppLabel(ResID_ConfigStrings, configStrings_BrowserAppLabel);
  148.  
  149.     // force this application to the front
  150.     ProcessSerialNumber lThisProcess;
  151.     if (::GetCurrentProcess(&lThisProcess) == noErr)
  152.     {
  153.         UProcesses::BringToFront(lThisProcess);
  154.     }
  155.  
  156.     UDesktop::Deactivate();
  157.     ThrowIfOSErr_(UAppleEvents::InteractWithUser(lNotificationRecord));
  158.     StPPCBrowser lBrowser(1, lTypes, outTarget, lDefaultSpecified, true, lPrompt, lAppLabel);
  159.     UDesktop::Activate();
  160.  
  161.     // whack up a wait cursor
  162.     StSpinningBeachBallCursor lBeachBall;
  163.  
  164.     // save the target ID into the preference file
  165.     {
  166.         StNewResource lResource(typeTargetID, ResID_TargetPreference, sizeof(TargetID), true);
  167.         ::BlockMoveData(&outTarget, *lResource.mResourceH, sizeof(TargetID));
  168.     }
  169. }
  170.  
  171. /* static */
  172. CAERepeater* CAERepeater::GetAERepeater(void)
  173. {
  174.     return (CAERepeater*)sModelDirector;
  175. }
  176.  
  177. /* static */
  178. void CAERepeater::PrintAEDesc(const AEDesc& inAEDesc)
  179. {
  180.     if (sDebuggingEnabled)
  181.     {
  182.         char* lBuffer = nil;
  183.         try
  184.         {
  185.             long lSize;
  186.             
  187.             ThrowIfOSErr_(::AEPrintSize((AEDesc*)&inAEDesc, &lSize));
  188.  
  189.             ThrowIfMemFail_(lBuffer = (char*)::NewPtr(lSize + 1));
  190.  
  191.             ThrowIfOSErr_(::AEPrint((AEDesc*)&inAEDesc, lBuffer, lSize));
  192.             
  193.             printf("--\n%s\n", lBuffer);
  194.  
  195.             ::DisposePtr(lBuffer);
  196.         }
  197.         catch(ExceptionCode inErr)
  198.         {
  199.             if (lBuffer != nil)
  200.             {
  201.                 ::DisposePtr(lBuffer);
  202.             }
  203.             
  204.             printf("Unable to write debug info : %d\n", inErr);
  205.         }
  206.     }
  207. }
  208.